const qcloud = require('../../vendor/qcloud-weapp-client-sdk/index.js')
const config = require('../../config.js')
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    paramData: {},
    department: 0,
    totalExcursion: 0,
    victory: 0,
    draw: 0,
    rUId: '',
    dId:"",
    averageWins: 0,
    shareState: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getUid(options)
  },


  getUid(options) {
    if (!!options) {
      this.obtainResultLast(options.dId)
    } else {
      wx.showToast({
        title: '未获取到部门信息',
        icon: 'fail',
        duration: 2000
      })
    }

    // if (app.globalData.uId != options.uId) {
    //   this.setData({
    //     'shareState': true
    //   })
    // } else {
    //   this.setData({
    //     'shareState': false
    //   })
    // }
  },

  // 返回计算率
  setcalculate(param) {
    this.setData({
      department: Math.floor(param.departmentRanking / param.departmentUserCount * 100)
    })

    this.setData({
      totalExcursion: Math.floor(param.bankRanking / param.bankUserCount * 100)
    })

    
    this.setData({
      victory: Math.floor(param.winCount / param.matchCount * 100)
    })

    this.setData({
      draw: Math.floor(param.participantCount / param.userCount * 100)
    })

    this.setData({
      averageWins: Math.floor(param.winCount / param.matchCount * 100) || 0
    })

    this.setData({
      avatarUrl: param.avatarUrl
    })

    this.setData({
      dId: app.globalData.dId
    })
  },

  obtainResultLast(dId) {
    qcloud.request({
      url: config.service.obtainMatchOrgRanking,
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      login: true,
      data: {
        'dId': dId
      },
      success: (response) => {
        let data = response.data
        if (data.code == 0) {
          this.setData({
            paramData: data.matchDepartmentStatDTO
          })
          this.setcalculate(this.data.paramData)
        } else {
          console.log(data.message)
        }
      },
      fail: function (err) {
        console.log(err);
      }
    })
  },

  // 分享

  onShareAppMessage: function () {
    return {
      title: '来看看我部门的成绩~',
      desc: '来看看我部门的成绩~',
      path: '/pages/officialRanking/officialRanking?dId=' + this.data.dId
    }
  }
})